import org.junit.Test;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class APITest {
    @Test
    public void test1() throws UnknownHostException {
        InetAddress address1 = InetAddress.getLocalHost();
        InetAddress address2 = InetAddress.getLoopbackAddress();

        System.out.println(address1.getHostName());
        System.out.println(address1.getHostAddress());
        System.out.println(address2.getHostName());
        System.out.println(address2.getHostAddress());
    }

    @Test
    public void test2() throws UnknownHostException {
        InetAddress address = InetAddress.getByName("www.baidu.com");
        System.out.println(address.getHostName());
        System.out.println(address.getHostAddress());
    }

    @Test
    public void test3() {
        String srcFileName = "picture.png";
        String dstFileName = "copy.png";
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFileName));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dstFileName))) {
            bis.transferTo(bos);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        System.out.println("复制完成");
    }

    @Test
    public void test4() {
        try (DatagramSocket datagramSocket = new DatagramSocket()) {
            InetAddress dstAddress = InetAddress.getLoopbackAddress();
            int dstPort = 18989;
            byte[] message = "我现在正在用UDP协议与你通信，你看见么？".getBytes(StandardCharsets.UTF_8);
            DatagramPacket packet = new DatagramPacket(message, message.length, dstAddress, dstPort);
            datagramSocket.send(packet);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test5() {
        final int port = 18989;

        try (DatagramSocket datagramSocket = new DatagramSocket(port)) {
            byte[] buffer = new byte[1024 * 64]; // 64kb
            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
            datagramSocket.receive(packet);
            System.out.println(new String(buffer, 0, packet.getLength()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test6() throws URISyntaxException {
        URI uri = URI.create("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%B0%91%E5%A5%B3&step_word=&hs=0&pn=265&spn=0&di=7412302663070515201&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=1331136520%2C1917588412&os=1528960216%2C3759778938&simid=3385064572%2C522197200&adpicid=0&lpn=0&ln=1864&fr=&fmq=1731426326599_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined&copyright=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201701%2F16%2F20170116114259_yQXud.thumb.700_0.jpeg%26refer%3Dhttp%3A%2F%2Fb-ssl.duitang.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1734018370%26t%3Dd0456a0d9abbeff3b4a006fe52691627&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B17tpwg2_z%26e3Bv54AzdH3Fks52AzdH3F%3Ft1%3Dmlcbm9ada&gsm=f0&rpstart=0&rpnum=0&islist=&querylist=&nojc=undefined&dyTabStr=MCwzLDEsMiwxMyw3LDYsNSwxMiw5&lid=8761959139686899042");
        System.out.println(uri.getUserInfo());
        System.out.println(uri.getHost());
        System.out.println(uri.getPort());
        System.out.println(uri.getAuthority());
        System.out.println(uri.getQuery());
        System.out.println(uri.getPath());
    }

    @Test
    public void test7() throws MalformedURLException {
        URI uri = URI.create("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%B0%91%E5%A5%B3&step_word=&hs=0&pn=265&spn=0&di=7412302663070515201&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=1331136520%2C1917588412&os=1528960216%2C3759778938&simid=3385064572%2C522197200&adpicid=0&lpn=0&ln=1864&fr=&fmq=1731426326599_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined&copyright=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201701%2F16%2F20170116114259_yQXud.thumb.700_0.jpeg%26refer%3Dhttp%3A%2F%2Fb-ssl.duitang.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1734018370%26t%3Dd0456a0d9abbeff3b4a006fe52691627&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B17tpwg2_z%26e3Bv54AzdH3Fks52AzdH3F%3Ft1%3Dmlcbm9ada&gsm=f0&rpstart=0&rpnum=0&islist=&querylist=&nojc=undefined&dyTabStr=MCwzLDEsMiwxMyw3LDYsNSwxMiw5&lid=8761959139686899042");
        URL url = uri.toURL();
        System.out.println(url.getAuthority());
        System.out.println(url.getDefaultPort());
        System.out.println(url.getFile());
        System.out.println(url.getProtocol());
    }

    @Test
    public void test8() throws IOException {
        URI uri = URI.create("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201701%2F16%2F20170116114259_yQXud.thumb.700_0.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1734020733&t=3475663b1b4ae2106a5f9d64bb1acb44");
        URL url = uri.toURL();
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("download.webp"));
             BufferedInputStream bis = new BufferedInputStream(url.openStream())) {
            bis.transferTo(bos);
        }
        System.out.println("下载完成");
    }

    @Test
    public void test9() {
        try (Socket socket = new Socket(InetAddress.getLoopbackAddress(), 18989);
             BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
             Scanner scanner = new Scanner(System.in)) {
            int cnt = 1;
            while (cnt-- > 0) {
                bos.write(scanner.nextLine().getBytes(StandardCharsets.UTF_8));
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test10() {
        try (ServerSocket serverSocket = new ServerSocket(18989)) {
            try (Socket socket = serverSocket.accept();
                 BufferedInputStream bis = new BufferedInputStream(socket.getInputStream())) {
                try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                    byte[] readBuffer = new byte[1024];
                    int len;
                    while ((len = bis.read(readBuffer, 0, readBuffer.length)) != -1) {
                        baos.write(readBuffer, 0, len);
                    }
                    System.out.println(baos.toString(StandardCharsets.UTF_8));
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test11() {
        try (Socket socket = new Socket(InetAddress.getLoopbackAddress(), 18989);
             BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream())) {
            String sendItem = "picture.png";
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sendItem))) {
                bis.transferTo(bos);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        System.out.println("客户端发送完毕");
    }

    @Test
    public void test12() {
        try (ServerSocket serverSocket = new ServerSocket(18989)) {
            try (Socket socket = serverSocket.accept();
                 BufferedInputStream bis = new BufferedInputStream(socket.getInputStream())) {
                String savePath = "receive_from_client.png";
                try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(savePath))) {
                    bis.transferTo(bos);
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        System.out.println("服务端接收完毕");
    }
}
